package com.sxx.beautifulcity.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.DeliciousFoodData;
import com.sxx.beautifulcity.entity.SpecialityData;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

public class DeliciousFoodAdapter extends RecyclerView.Adapter<
        DeliciousFoodAdapter.DeliciousFoodAdapterViewHolder> {

    private List<DeliciousFoodData> list;
    private OnMyItemClickListener listener;

    public void setOnMyItemClick(OnMyItemClickListener l){
        listener = l;
    }

    /**
     * Created by dllo on 15/10/23.
     * 美食
     */

    public DeliciousFoodAdapter(Context context) {

    }

    /***
     * 加载数据
     * @param datas
     */
    public void addData(List<DeliciousFoodData> datas){
        list = datas;
        notifyDataSetChanged();
    }


    /****
     * 初始化组件
     */
    class DeliciousFoodAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private SimpleDraweeView fine_food_simpleDv;
        private TextView fine_food_nameTv;
        private RatingBar find_food_ratingBar;
        private LinearLayout delicious_linearlayout;
        private int position;

        public DeliciousFoodAdapterViewHolder(View v) {
            super(v);
            fine_food_simpleDv = (SimpleDraweeView) v.findViewById(R.id.fine_food_simpleDv);
            fine_food_nameTv = (TextView) v.findViewById(R.id.fine_food_nameTv);
            find_food_ratingBar = (RatingBar) v.findViewById(R.id.find_food_ratingBar);
            delicious_linearlayout = (LinearLayout) v.findViewById(R.id.delicious_linearlayout);
            delicious_linearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                DeliciousFoodData data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }

    /***
     * 加载行布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public DeliciousFoodAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_fine_food_item, null);

        return new DeliciousFoodAdapterViewHolder(v);
    }

    /***
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(DeliciousFoodAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            DeliciousFoodData data = list.get(position);
            holder.fine_food_nameTv.setText(data.getName());
            holder.find_food_ratingBar.setNumStars(data.getStar());

            if (data.getImage() != null && data.getImage().length() > 0) {
                String path = data.getImage();
                frescoRequestImage(path , holder.fine_food_simpleDv);
            }

        }
        holder.position = position;

    }

    /***
     * 请求网络图片  三级缓存
     * @return
     */

   public void frescoRequestImage(String path , SimpleDraweeView draweeView){
       Uri uri = Uri.parse(path);
       draweeView.setImageURI(uri);
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

}
